package com.blog.dao.frontpage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.blog.dao.conn.BaseDao;
import com.blog.dao.conn.ParamSet;
import com.blog.dao.conn.RowDatas;
import com.blog.entity.DiscussConstraintEntity;
import com.blog.entity.DiscussEntity;

public class DiscussDao extends BaseDao{

	private RowDatas<DiscussConstraintEntity> discuss = new RowDatas<DiscussConstraintEntity>() {

		@Override
		public DiscussConstraintEntity getRowData(ResultSet rs) throws SQLException {
			DiscussConstraintEntity de = new DiscussConstraintEntity();
			de.setArticle_id(rs.getInt("article_id"));
			de.setDiscuss_content(rs.getString("discuss_content"));
			de.setDiscuss_time(rs.getTimestamp("discuss_time"));
			de.setUser_image(rs.getString("user_image"));
			de.setUser_name(rs.getString("user_name"));
			return de;
		}
	};
	
	//向数据库插入一条数据
	public int insertDiscuss(DiscussEntity discuss) {
		int row = 0;
		String sql = "insert into tb_discuss(discuss_id,article_id,discuss_content,discuss_time)\r\n" + 
				"VALUES(?,?,?,?)";
		row = updateExecuted(sql, new ParamSet(discuss.getDiscuss_id(),discuss.getArticle_id(),discuss.getDiscuss_content(),discuss.getDiscuss_time()));
		return row;
		
	}

	public List<DiscussConstraintEntity> queryAllDiscuss(int articleid) {
		String sql = "select u.user_name,u.user_image,d.article_id,d.discuss_content,d.discuss_time \r\n" + 
				"from tb_discuss d LEFT JOIN tb_user u on d.discuss_id = u.user_id\r\n" + 
				"where d.article_id = "+articleid;
		return queryList(sql, discuss);
		
	}
}
